package com.dyzx.hbb.modules.wms.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.common.utils.Utils;
import com.dyzx.hbb.modules.wms.entity.WmsHouse;
import com.dyzx.hbb.modules.wms.entity.WmsLocation;
import com.dyzx.hbb.modules.wms.entity.WmsLocationArea;
import com.dyzx.hbb.modules.wms.query.WmsLocationAreaQuery;
import com.dyzx.hbb.modules.wms.service.WmsHouseService;
import com.dyzx.hbb.modules.wms.service.WmsLocationAreaService;
import com.dyzx.hbb.modules.wms.service.WmsLocationService;
import com.dyzx.hbb.modules.wms.vo.WmsLocationAreaVO;
import com.dyzx.hbb.modules.wms.vo.WmsLocationAreaUVO;
import com.dyzx.hbb.security.context.SecurityContextHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 货位区域管理控制器
 * <p>处理仓库管理系统中货位区域的创建、查询和管理业务逻辑
 * <p>主要职责：
 * <ul>
 * <li>提供货位区域的基础CRUD操作</li>
 * <li>支持按名称、货位、编号、状态等条件筛选货位区域信息</li>
 * <li>验证货位区域所属货位的有效性</li>
 * <li>提供货位区域的简易列表接口</li>
 * <li>支持按货位筛选的货位区域列表查询</li>
 * <li>基于货位编号自动生成货位区域编号</li>
 * </ul>
 * <p>设计意图：为仓库管理模块提供精细化的货位区域管理接口，
 * 实现仓库空间的层次化管理，提高库存管理的精确性和效率
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2023-05-30
 */
@Tag(name = "货位区域管理", description = "货位区域相关接口")
@RestController
@RequestMapping("/wms/location/area")
public class WmsLocationAreaController extends BaseValidController<
        WmsLocationAreaService,
        WmsLocationArea,
        WmsLocationAreaQuery,
        WmsLocationAreaVO,
        WmsLocationAreaUVO
        > {

    @Autowired
    private WmsLocationAreaService service;
    @Autowired
    private WmsLocationService locationService;

    @Override
    protected WmsLocationAreaService getBaseService() {
        return service;
    }

    @Override
    protected MyLambdaQueryWrapper<WmsLocationArea> queryWrapper(WmsLocationAreaQuery query) {
        MyLambdaQueryWrapper<WmsLocationArea> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmsLocationArea::getCreateDate);

        if (query != null) {
            queryWrapper.likeRight(StringUtils.isNotEmpty(query.getName()), WmsLocationArea::getName, query.getName())
                    .eq(query.getLocation_id() != null, WmsLocationArea::getLocationId, query.getLocation_id())

                    .likeRight(StringUtils.isNotEmpty(query.getNo()), WmsLocationArea::getNo, query.getNo())
                    .eq(query.getStatus() != null, WmsLocationArea::getStatus, query.getStatus());
        }
        return queryWrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<WmsLocationArea> queryInfoWrapper() {
        MyLambdaQueryWrapper<WmsLocationArea> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmsLocationArea::getCreateDate);
        return queryWrapper;
    }

    @Override
    protected WmsLocationArea addValid(WmsLocationAreaVO entity) {
        WmsLocation byId = locationService.getById(entity.getLocationId());
        if (byId == null) {
        throw new RuntimeException("货位不存在");
        }
        Long organId = SecurityContextHolder.getOrganId();
        MyLambdaQueryWrapper<WmsLocationArea> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(WmsLocationArea::getOrganId, organId).eq(WmsLocationArea::getLocationId, entity.getLocationId());
        long count = service.count(queryWrapper);
        String no = Utils.generateYMOrderNumber(byId.getNo(), count);

        WmsLocationArea wmsHouse = new WmsLocationArea();
        BeanUtil.copyProperties(entity, wmsHouse);
        wmsHouse.setNo(no);

        return wmsHouse;
    }

    @Override
    protected WmsLocationArea editValid(WmsLocationAreaUVO entity) {
        return null;
    }
    @Operation(summary = "简易列表")
    @GetMapping("/mini_list_data")
    public Result<List<Map<String, Object>>> miniListData(
            @RequestParam(value = "location_id", required = false) long location_id
    ) {
        if (location_id <= 0) {
            throw new RuntimeException("货位不存在");
        }
        MyLambdaQueryWrapper<WmsLocationArea> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(WmsLocationArea::getId, WmsLocationArea::getName,  WmsLocationArea::getRoomNum, WmsLocationArea::getFloor);
        wrapper.select(WmsLocationArea::getName).as("label");
        wrapper.select(WmsLocationArea::getId).as("value");
        wrapper.orderByDesc(WmsLocationArea::getId);
        wrapper.eq(WmsLocationArea::getLocationId, location_id);


        List<Map<String, Object>> list = getBaseService().listMaps(wrapper);
        return list.isEmpty() ? Result.error(404, "暂无数据") : Result.ok(list);
    }

    @Operation(summary = "简易列表")
    @GetMapping("/mini_list")
    public Result<List<Map<String, Object>>> miniList(
    ) {
        MyLambdaQueryWrapper<WmsLocationArea> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(WmsLocationArea::getId, WmsLocationArea::getName,  WmsLocationArea::getRoomNum, WmsLocationArea::getFloor);
        wrapper.select(WmsLocationArea::getName).as("label");
        wrapper.select(WmsLocationArea::getId).as("value");
        wrapper.orderByDesc(WmsLocationArea::getId);


        List<Map<String, Object>> list = getBaseService().listMaps(wrapper);
        return list.isEmpty() ? Result.error(404, "暂无数据") : Result.ok(list);
    }
}